<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/img/avatar/pear.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/img/avatar/pear-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/img/avatar/pear-16x16.png">
  <link rel="mask-icon" href="/img/avatar/pear.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=FiraGo:300,300italic,400,400italic,700,700italic%7CSource+Han+Sans+SC:300,300italic,400,400italic,700,700italic%7CFira+Code:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"syvshc.gitee.io","root":"/","images":"/images","scheme":"Gemini","version":"8.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"buttons","active":"gitalk","storage":true,"lazyload":true,"nav":{"gitalk":{"order":-2}},"activeClass":"gitalk"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":5,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="在群里遇到一个需求: 使用 ctexart 文档类排版试卷, 使用 enumerate 环境生成题目并对题目计数, 希望在每个 \section 中显示&quot;本节共xx题&quot;, 并在目录中显示, 其中 xx 为该节中的一级列表环境中的 item 数量. 下面来进行实现.">
<meta property="og:type" content="article">
<meta property="og:title" content="统计一节中的列表项数量">
<meta property="og:url" content="https://syvshc.gitee.io/2021-11-10-compute-total-items/index.html">
<meta property="og:site_name" content="无锤乙醇">
<meta property="og:description" content="在群里遇到一个需求: 使用 ctexart 文档类排版试卷, 使用 enumerate 环境生成题目并对题目计数, 希望在每个 \section 中显示&quot;本节共xx题&quot;, 并在目录中显示, 其中 xx 为该节中的一级列表环境中的 item 数量. 下面来进行实现.">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/syvshc/image/master/postimg/compute-total-items/new.png">
<meta property="og:image" content="https://raw.githubusercontent.com/syvshc/image/master/postimg/compute-total-items/20211111094748.png">
<meta property="article:published_time" content="2021-11-10T03:47:24.000Z">
<meta property="article:modified_time" content="2021-11-19T04:47:46.610Z">
<meta property="article:author" content="Zhonghao Sun">
<meta property="article:tag" content="LaTeX">
<meta property="article:tag" content="示例">
<meta property="article:tag" content="列表相关">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://raw.githubusercontent.com/syvshc/image/master/postimg/compute-total-items/new.png">


<link rel="canonical" href="https://syvshc.gitee.io/2021-11-10-compute-total-items/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://syvshc.gitee.io/2021-11-10-compute-total-items/","path":"2021-11-10-compute-total-items/","title":"统计一节中的列表项数量"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>统计一节中的列表项数量 | 无锤乙醇</title>
  
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-127047099-2","only_pageview":true}</script>
  <script src="/js/third-party/analytics/google-analytics.js"></script>




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">无锤乙醇</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">学而未精</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E6%80%9D%E8%B7%AF"><span class="nav-number">1.</span> <span class="nav-text">实现思路</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0-%E6%96%B0"><span class="nav-number">2.</span> <span class="nav-text">具体实现 (新)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E4%BE%8B-%E6%96%B0"><span class="nav-number">3.</span> <span class="nav-text">实例 (新)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0-%E6%97%A7"><span class="nav-number">4.</span> <span class="nav-text">具体实现 (旧)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E4%BE%8B-%E6%97%A7"><span class="nav-number">5.</span> <span class="nav-text">实例 (旧)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B7%B2%E7%9F%A5%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">6.</span> <span class="nav-text">已知的问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%8E%E7%BB%AD"><span class="nav-number">7.</span> <span class="nav-text">后续</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Zhonghao Sun"
      src="/img/avatar/qqavatar.jpg">
  <p class="site-author-name" itemprop="name">Zhonghao Sun</p>
  <div class="site-description" itemprop="description">从未想过背叛, 也不必说忠贞</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">17</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/syvshc" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;syvshc" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:syvshc@foxmail.com" title="E-Mail → mailto:syvshc@foxmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://tex.stackexchange.com/users/180617/syvshc" title="TeX.SE → https:&#x2F;&#x2F;tex.stackexchange.com&#x2F;users&#x2F;180617&#x2F;syvshc" rel="noopener" target="_blank"><i class="fab fa-stack-exchange fa-fw"></i>TeX.SE</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://studio.youtube.com/channel/UCqvoLd2tlB_xuRkZ7_GYOVw/editing/details" title="YouTube → https:&#x2F;&#x2F;studio.youtube.com&#x2F;channel&#x2F;UCqvoLd2tlB_xuRkZ7_GYOVw&#x2F;editing&#x2F;details" rel="noopener" target="_blank"><i class="fab fa-youtube fa-fw"></i>YouTube</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.worldcubeassociation.org/persons/2015SUNZ03/" title="WCA → https:&#x2F;&#x2F;www.worldcubeassociation.org&#x2F;persons&#x2F;2015SUNZ03&#x2F;" rel="noopener" target="_blank"><i class="fas fa-cubes fa-fw"></i>WCA</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://steamcommunity.com/id/syvshc" title="Steam → https:&#x2F;&#x2F;steamcommunity.com&#x2F;id&#x2F;syvshc" rel="noopener" target="_blank"><i class="fab fa-steam fa-fw"></i>Steam</a>
      </span>
  </div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdn.jsdelivr.net/npm/@creativecommons/vocabulary@2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fas fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.latexstudio.net/" title="https:&#x2F;&#x2F;www.latexstudio.net" rel="noopener" target="_blank">LaTeX 工作室</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://sikouhjw.github.io/" title="https:&#x2F;&#x2F;sikouhjw.github.io&#x2F;" rel="noopener" target="_blank">sikouhjw</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://zhuanlan.zhihu.com/typography-and-latex/" title="https:&#x2F;&#x2F;zhuanlan.zhihu.com&#x2F;typography-and-latex&#x2F;" rel="noopener" target="_blank">muzimuzhi</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://liam.page/" title="https:&#x2F;&#x2F;liam.page&#x2F;" rel="noopener" target="_blank">Liam</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://stone-zeng.github.io/" title="https:&#x2F;&#x2F;stone-zeng.github.io&#x2F;" rel="noopener" target="_blank">stone-zeng</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://muyuuuu.github.io/" title="https:&#x2F;&#x2F;muyuuuu.github.io&#x2F;" rel="noopener" target="_blank">兰铃</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yuxtech.club/" title="http:&#x2F;&#x2F;yuxtech.club&#x2F;" rel="noopener" target="_blank">yuxtech</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://camusecao.top/" title="https:&#x2F;&#x2F;camusecao.top&#x2F;" rel="noopener" target="_blank">CamuseCao</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.alanshawn.com/" title="https:&#x2F;&#x2F;www.alanshawn.com&#x2F;" rel="noopener" target="_blank">Alan Xiang</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://ssayno.github.io/" title="https:&#x2F;&#x2F;ssayno.github.io&#x2F;" rel="noopener" target="_blank">ssayno</a>
        </li>
    </ul>
  </div>

          </div>
        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/syvshc" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://syvshc.gitee.io/2021-11-10-compute-total-items/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/img/avatar/qqavatar.jpg">
      <meta itemprop="name" content="Zhonghao Sun">
      <meta itemprop="description" content="从未想过背叛, 也不必说忠贞">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="无锤乙醇">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          统计一节中的列表项数量
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-11-10 11:47:24" itemprop="dateCreated datePublished" datetime="2021-11-10T11:47:24+08:00">2021-11-10</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-11-19 12:47:46" itemprop="dateModified" datetime="2021-11-19T12:47:46+08:00">2021-11-19</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/LaTeX/" itemprop="url" rel="index"><span itemprop="name">LaTeX</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/LaTeX/%E5%88%97%E8%A1%A8%E7%9B%B8%E5%85%B3/" itemprop="url" rel="index"><span itemprop="name">列表相关</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>9.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>9 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>在群里遇到一个需求: 使用 <code>ctexart</code> 文档类排版试卷, 使用 <code>enumerate</code> 环境生成题目并对题目计数, 希望在每个 <code>\section</code> 中显示"本节共xx题", 并在目录中显示, 其中 xx 为该节中的一级列表环境中的 item 数量.</p>
<p>下面来进行实现.</p>
<span id="more"></span>
<p>根据我在 <a href="tex.stackexchange.com">tex.se</a> 上的 <a target="_blank" rel="noopener" href="https://tex.stackexchange.com/q/622028/180617">提问</a> 进行修改:</p>
<h2 id="实现思路">实现思路</h2>
<ol type="1">
<li>使用计数器 <code>totalitems</code> 来统计每一节的 <code>\item</code> 的数量之和</li>
<li>在一节的末尾使用命令将这一节的 <code>totalitems</code> 的值输出到辅助文件 <code>main.aux</code></li>
<li>第二次编译的时候再在 <code>section</code> 中调用该计数器来显示题目的数量</li>
</ol>
<h2 id="具体实现-新">具体实现 (新)</h2>
<p>在发现了可以判断层级的命令后, 思路就可以打开了, 这次我们直接修改 <code>\item</code> 命令, 让第一层级内的 <code>\item</code> 进行记数, 然后再处理嵌套以及 <code>\item</code> 的参数问题. 我更喜欢这种方式.</p>
<ol type="1">
<li><p>添加判断命令 <code>\ifenum</code> 来判断是否处于第一层级</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\newif\ifenum</span><br></pre></td></tr></table></figure></p></li>
<li><p>这次我们只需要定义一个计数器</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">\newcounter&#123;totalitems&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>存储旧的 <code>enumerate</code> 环境以及 <code>\item</code> 命令</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">\usepackage&#123;letltxmacro&#125;</span><br><span class="line">\LetLtxMacro&#123;\@numerate&#125;&#123;\enumerate&#125;</span><br><span class="line">\LetLtxMacro&#123;\end@numerate&#125;&#123;\endenumerate&#125;</span><br><span class="line">\LetLtxMacro&#123;\it@m&#125;&#123;\item&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>重定义 <code>\item</code> 命令, 使它具有以下功能: 如果使用 <code>\item*</code> 则不进行计数, 同时可以使用 <code>\item[para]</code> 来进行自定义序号, 如可以写 <code>\item[附加题]</code> 等等, 该功能默认计数, 如果不想计数也可以使用 <code>\item*[para]</code> 来取消计数. <strong>更新: 需要将 <code>\IfBooleanT</code> 放在 <code>\ifenum</code> 内部, 防止在非第一层级中误用 <code>\item*</code> 造成的计数错误</strong>:</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">\DeclareDocumentCommand&#123;\item&#125;&#123; s o &#125;&#123;</span><br><span class="line">    \IfNoValueTF&#123;#2&#125;&#123;\it@m&#125;&#123;\it@m[#2]&#125;</span><br><span class="line">    \ifenum</span><br><span class="line">        \IfBooleanT&#123;#1&#125;&#123;\addtocounter&#123;totalitems&#125;&#123;-1&#125;&#125;%</span><br><span class="line">        \stepcounter&#123;totalitems&#125;</span><br><span class="line">    \fi</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>然后来确定什么时候打开和关闭判断的开关: 由于嵌套的存在, 我们需要在非第一层的环境开始时关闭 <code>\ifenum</code>, 并在非第一层的环境结束时开启 <code>\ifenum</code>. 如果出现了多层嵌套或者连续嵌套也不会影响, 因为 <code>\ifenum</code> 总会在非第一层环境的开始时被关闭:</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">\renewcommand&#123;\enumerate&#125;[1][]&#123;%</span><br><span class="line">    \@numerate[#1]</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne</span><br><span class="line">        \enumtrue</span><br><span class="line">    \else</span><br><span class="line">        \enumfalse</span><br><span class="line">    \fi</span><br><span class="line">&#125;</span><br><span class="line">\renewcommand&#123;\endenumerate&#125;&#123;</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne </span><br><span class="line">        \enumfalse</span><br><span class="line">    \else</span><br><span class="line">        \enumtrue</span><br><span class="line">    \fi</span><br><span class="line">    \end@numerate%</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>在一节中题目结束的位置使用 <code>\exerend</code> 命令来将此时的 <code>totalitems</code> 的值输出到辅助文件, 写入为形如 <code>\gdef\totalenumi@i&#123;4&#125;</code> 的全局命令</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">\def\exerend&#123;</span><br><span class="line">    \immediate\write\@mainaux&#123;%</span><br><span class="line">        \string\gdef\string\totalenumi@\romannumeral%</span><br><span class="line">        \csname c@section\endcsname</span><br><span class="line">        &#123;\number\csname c@totalitems\endcsname&#125;</span><br><span class="line">    &#125;%</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>在每个 <code>\section</code> 开始的时候都将 <code>totalitems</code> 的值重置为 0</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">\let\s@ction\section</span><br><span class="line">\def\section&#123;</span><br><span class="line">    \setcounter&#123;totalitems&#125;&#123;0&#125;\s@ction</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>定义输出总题数的命令 <code>\exer@sectoin&#123;&#125;</code>, 注意到在第一次编译的时候 <code>\totalenumi@i</code> 是没有定义的, 需要进行判断</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">\def\exer@section#1&#123;\@ifundefined&#123;totalenumi@\romannumeral #1&#125;%</span><br><span class="line">    &#123;0&#125;%</span><br><span class="line">    &#123;\csname totalenumi@\romannumeral #1\endcsname&#125;%</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>定义输出总题数的命令:</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">\def\outputexer&#123;</span><br><span class="line">    (There \ifnum \exer@section&#123;\c@section&#125;&#x3D;0%</span><br><span class="line">        is ??&#123;&#125; exercise%</span><br><span class="line">    \else%</span><br><span class="line">        \ifnum \exer@section&#123;\c@section&#125;&#x3D;1%</span><br><span class="line">            is 1 exercise%</span><br><span class="line">        \else%</span><br><span class="line">            are \exer@section&#123;\c@section&#125; exercises%</span><br><span class="line">        \fi%</span><br><span class="line">    \fi.)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>由于英文中有 is 和 are 以及单复数的关系, 就需要多一层判断. 如果是中文的话, 连带着上一个命令可以直接写为</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">\def\exer@sectionzh#1&#123;%</span><br><span class="line">\@ifundefined&#123;totalenumi@\romannumeral #1&#125;%</span><br><span class="line">    &#123;??&#125;%</span><br><span class="line">    &#123;\csname totalenumi@\romannumeral #1\endcsname&#125;%</span><br><span class="line">&#125;</span><br><span class="line">\def\outputexerzh&#123;%</span><br><span class="line">    (本节共有 \exer@sectionzh&#123;\c@section&#125;题.)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p></li>
</ol>
<h2 id="实例-新">实例 (新)</h2>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line">\documentclass&#123;ctexart&#125;</span><br><span class="line">\usepackage&#123;enumitem&#125;</span><br><span class="line">\usepackage[margin&#x3D;2.5cm]&#123;geometry&#125;</span><br><span class="line">\makeatletter</span><br><span class="line">\newif\ifenum</span><br><span class="line"></span><br><span class="line">\newcounter&#123;totalitems&#125;</span><br><span class="line">\usepackage&#123;letltxmacro&#125;</span><br><span class="line">\LetLtxMacro&#123;\@numerate&#125;&#123;\enumerate&#125;</span><br><span class="line">\LetLtxMacro&#123;\end@numerate&#125;&#123;\endenumerate&#125;</span><br><span class="line">\LetLtxMacro&#123;\it@m&#125;&#123;\item&#125;</span><br><span class="line">\DeclareDocumentCommand&#123;\item&#125;&#123; s o &#125;&#123;</span><br><span class="line">    \IfNoValueTF&#123;#2&#125;&#123;\it@m&#125;&#123;\it@m[#2]&#125;</span><br><span class="line">    \ifenum</span><br><span class="line">        \IfBooleanT&#123;#1&#125;&#123;\addtocounter&#123;totalitems&#125;&#123;-1&#125;&#125;%</span><br><span class="line">        \stepcounter&#123;totalitems&#125;</span><br><span class="line">    \fi</span><br><span class="line">&#125;</span><br><span class="line">\renewcommand&#123;\enumerate&#125;[1][]&#123;%</span><br><span class="line">    \@numerate[#1]</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne</span><br><span class="line">        \enumtrue</span><br><span class="line">    \else</span><br><span class="line">        \enumfalse</span><br><span class="line">    \fi</span><br><span class="line">&#125;</span><br><span class="line">\renewcommand&#123;\endenumerate&#125;&#123;</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne </span><br><span class="line">        \enumfalse</span><br><span class="line">    \else</span><br><span class="line">        \enumtrue</span><br><span class="line">    \fi</span><br><span class="line">    \end@numerate%</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\def\exerend&#123;</span><br><span class="line">    \immediate\write\@mainaux&#123;%</span><br><span class="line">        \string\gdef\string\totalenumi@\romannumeral%</span><br><span class="line">        \csname c@section\endcsname</span><br><span class="line">        &#123;\number\csname c@totalitems\endcsname&#125;</span><br><span class="line">    &#125;%</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\let\s@ction\section</span><br><span class="line">\def\section&#123;</span><br><span class="line">    \setcounter&#123;totalitems&#125;&#123;0&#125;\s@ction</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\def\exer@section#1&#123;\@ifundefined&#123;totalenumi@\romannumeral #1&#125;%</span><br><span class="line">    &#123;0&#125;%</span><br><span class="line">    &#123;\csname totalenumi@\romannumeral #1\endcsname&#125;%</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\def\outputexer&#123;</span><br><span class="line">    (There \ifnum \exer@section&#123;\c@section&#125;&#x3D;0%</span><br><span class="line">        is ??&#123;&#125; exercise%</span><br><span class="line">    \else%</span><br><span class="line">        \ifnum \exer@section&#123;\c@section&#125;&#x3D;1%</span><br><span class="line">            is 1 exercise%</span><br><span class="line">        \else%</span><br><span class="line">            are \exer@section&#123;\c@section&#125; exercises%</span><br><span class="line">        \fi%</span><br><span class="line">    \fi.)</span><br><span class="line">&#125;</span><br><span class="line">\def\exer@sectionzh#1&#123;%</span><br><span class="line">\@ifundefined&#123;totalenumi@\romannumeral #1&#125;%</span><br><span class="line">    &#123;??&#125;%</span><br><span class="line">    &#123;\csname totalenumi@\romannumeral #1\endcsname&#125;%</span><br><span class="line">&#125;</span><br><span class="line">\def\outputexerzh&#123;%</span><br><span class="line">    (本节共有 \exer@sectionzh&#123;\c@section&#125;题.)</span><br><span class="line">&#125;</span><br><span class="line">\makeatother</span><br><span class="line"></span><br><span class="line">\begin&#123;document&#125;</span><br><span class="line">\tableofcontents</span><br><span class="line">\section&#123;Optional, nested, and resume\outputexer&#125;</span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">    \begin&#123;enumerate&#125;</span><br><span class="line">        \item this is the second level of enumerate</span><br><span class="line">        \begin&#123;enumerate&#125;</span><br><span class="line">            \item this is the third level of enumerate</span><br><span class="line">        \end&#123;enumerate&#125;</span><br><span class="line">        \item* this is a star verion in the second level, will do nothing</span><br><span class="line">    \end&#123;enumerate&#125;</span><br><span class="line">    \item[option] this is an optional paramater without star.</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line"></span><br><span class="line">\begin&#123;enumerate&#125;[resume]</span><br><span class="line">    \item this is a resume version</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line">\exerend</span><br><span class="line"></span><br><span class="line">\section&#123;带星号以及可选参数\outputexerzh&#125;</span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">    \item*[option] this is a star version with an optional paramater.</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line"></span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">    \item* this is a star version</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line">\exerend</span><br><span class="line"></span><br><span class="line">\section&#123;A regular enumerate&#125; </span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line"></span><br><span class="line">\section&#123;A single item\outputexer&#125;</span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item just one item</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line">\exerend</span><br><span class="line">\end&#123;document&#125;</span><br></pre></td></tr></table></figure>
<p>使用 <code>xelatex</code> 编译三次后可以输出</p>
<figure>
<img src="https://raw.githubusercontent.com/syvshc/image/master/postimg/compute-total-items/new.png" alt="new output" /><figcaption aria-hidden="true">new output</figcaption>
</figure>
<h2 id="具体实现-旧">具体实现 (旧)</h2>
<ol type="1">
<li><p>新定义计数器 <code>totalitems</code>, <code>itemsinenum</code>, <code>beginitems</code>, <code>enditems</code>. 分别用来存放: 一节中的所有 item 的数量, 一个 <code>enumerate</code> 环境中的 item 数量, <code>enumerate</code> 环境开始以及结束时的 <code>enumi</code> 的值.</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">\newcounter&#123;totalitems&#125;</span><br><span class="line">\newcounter&#123;itemsinenum&#125;</span><br><span class="line">\newcounter&#123;beginitems&#125;</span><br><span class="line">\newcounter&#123;enditems&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>重新定义 <code>enumerate</code> 环境, 使其可以添加可选参数, 并且在开始与结束时记录 <code>enumi</code> 的值, 使用 <code>itemsinenum</code> 计数器来存储二者之差, 也就是当前环境中 item 的数量, 然后把这个数量添加到 <code>totalitems</code> 计数器中. 这里使用了 <code>\enit@depth</code> 命令来判断是否处于最高级的 <code>enumerate</code> 环境中</p>
<p><figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">\usepackage&#123;letltxmacro&#125;</span><br><span class="line">\LetLtxMacro&#123;\@numerate&#125;&#123;\enumerate&#125;</span><br><span class="line">\LetLtxMacro&#123;\end@numerate&#125;&#123;\endenumerate&#125;</span><br><span class="line"></span><br><span class="line">\renewcommand&#123;\enumerate&#125;[1][]&#123;%</span><br><span class="line">    \@numerate[#1]</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne </span><br><span class="line">        \setcounter&#123;beginitems&#125;&#123;\arabic&#123;enumi&#125;&#125;</span><br><span class="line">    \fi</span><br><span class="line">&#125;</span><br><span class="line">\renewcommand&#123;\endenumerate&#125;&#123;</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne </span><br><span class="line">        \setcounter&#123;enditems&#125;&#123;\arabic&#123;enumi&#125;&#125;</span><br><span class="line">        \setcounter&#123;itemsinenum&#125;&#123;\numexpr \c@enditems - \c@beginitems \relax&#125;</span><br><span class="line">        \addtocounter&#123;totalitems&#125;&#123;\arabic&#123;itemsinenum&#125;&#125;</span><br><span class="line">    \fi</span><br><span class="line">    \end@numerate%</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p></li>
<li><p>接下来就是记录题数, 输出题数的过程, 与上文相同, 这里不多赘述.</p></li>
</ol>
<h2 id="实例-旧">实例 (旧)</h2>
<figure class="highlight tex"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line">\documentclass&#123;ctexart&#125;</span><br><span class="line">\usepackage&#123;enumitem&#125;</span><br><span class="line">\makeatletter</span><br><span class="line">\newcounter&#123;totalitems&#125;</span><br><span class="line">\newcounter&#123;itemsinenum&#125;</span><br><span class="line">\newcounter&#123;beginitems&#125;</span><br><span class="line">\newcounter&#123;enditems&#125;</span><br><span class="line"></span><br><span class="line">\usepackage&#123;letltxmacro&#125;</span><br><span class="line">\LetLtxMacro&#123;\@numerate&#125;&#123;\enumerate&#125;</span><br><span class="line">\LetLtxMacro&#123;\end@numerate&#125;&#123;\endenumerate&#125;</span><br><span class="line"></span><br><span class="line">\renewcommand&#123;\enumerate&#125;[1][]&#123;%</span><br><span class="line">    \@numerate[#1]</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne </span><br><span class="line">        \setcounter&#123;beginitems&#125;&#123;\arabic&#123;enumi&#125;&#125;</span><br><span class="line">    \fi</span><br><span class="line">&#125;</span><br><span class="line">\renewcommand&#123;\endenumerate&#125;&#123;</span><br><span class="line">    \ifnum\enit@depth&#x3D;\@ne </span><br><span class="line">        \setcounter&#123;enditems&#125;&#123;\arabic&#123;enumi&#125;&#125;</span><br><span class="line">        \setcounter&#123;itemsinenum&#125;&#123;\numexpr \c@enditems - \c@beginitems \relax&#125;</span><br><span class="line">        \addtocounter&#123;totalitems&#125;&#123;\arabic&#123;itemsinenum&#125;&#125;</span><br><span class="line">    \fi</span><br><span class="line">    \end@numerate%</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\def\exerend&#123;</span><br><span class="line">    \immediate\write\@mainaux&#123;%</span><br><span class="line">        \string\gdef\string\totalenumi@\romannumeral%</span><br><span class="line">        \csname c@section\endcsname</span><br><span class="line">        &#123;\number\csname c@totalitems\endcsname&#125;</span><br><span class="line">    &#125;%</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\let\s@ction\section</span><br><span class="line">\def\section&#123;</span><br><span class="line">    \setcounter&#123;totalitems&#125;&#123;0&#125;\s@ction</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\def\exer@section#1&#123;\@ifundefined&#123;totalenumi@\romannumeral #1&#125;%</span><br><span class="line">    &#123;0&#125;%</span><br><span class="line">    &#123;\csname totalenumi@\romannumeral #1\endcsname&#125;%</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\def\outputexer&#123;</span><br><span class="line">    (There \ifnum \exer@section&#123;\c@section&#125;&#x3D;0%</span><br><span class="line">        is ??&#123;&#125; exercise%</span><br><span class="line">    \else%</span><br><span class="line">        \ifnum \exer@section&#123;\c@section&#125;&#x3D;1%</span><br><span class="line">            is 1 exercise%</span><br><span class="line">        \else%</span><br><span class="line">            are \exer@section&#123;\c@section&#125; exercises%</span><br><span class="line">        \fi%</span><br><span class="line">    \fi.)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">\def\exer@sectionzh#1&#123;%</span><br><span class="line">\@ifundefined&#123;totalenumi@\romannumeral #1&#125;%</span><br><span class="line">    &#123;??&#125;%</span><br><span class="line">    &#123;\csname totalenumi@\romannumeral #1\endcsname&#125;%</span><br><span class="line">&#125;</span><br><span class="line">\def\outputexerzh&#123;%</span><br><span class="line">    (本节共有 \exer@sectionzh&#123;\c@section&#125;题.)</span><br><span class="line">&#125;</span><br><span class="line">\makeatother</span><br><span class="line">\begin&#123;document&#125;</span><br><span class="line">\tableofcontents</span><br><span class="line">\section&#123;One\outputexer&#125;</span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">    \begin&#123;enumerate&#125;</span><br><span class="line">        \item this is the second level of enumerate</span><br><span class="line">    \end&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line"></span><br><span class="line">\begin&#123;enumerate&#125;[resume]</span><br><span class="line">    \item test</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line">\exerend</span><br><span class="line"></span><br><span class="line">\section&#123;Two\outputexerzh&#125;</span><br><span class="line">\begin&#123;enumerate&#125;[start&#x3D;5]</span><br><span class="line">    \item test</span><br><span class="line">    \item test</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line"></span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line">\exerend</span><br><span class="line"></span><br><span class="line">\section&#123;No output&#125; </span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line"></span><br><span class="line">\section&#123;Three\outputexer&#125;</span><br><span class="line">\begin&#123;enumerate&#125;</span><br><span class="line">    \item test</span><br><span class="line">\end&#123;enumerate&#125;</span><br><span class="line">\exerend</span><br><span class="line">\end&#123;document&#125;</span><br></pre></td></tr></table></figure>
<p>三次 <code>xelatex</code> 编译后显示效果为</p>
<figure>
<img src="https://raw.githubusercontent.com/syvshc/image/master/postimg/compute-total-items/20211111094748.png" alt="output" /><figcaption aria-hidden="true">output</figcaption>
</figure>
<h2 id="已知的问题"><del>已知的问题</del></h2>
<ol type="1">
<li><p><del>当一节中出现带 <code>[resume]</code> 选项的 <code>enumerate</code> 环境的时候, <code>totalitems</code> 会将两个环境结束时的 <code>enumi</code> 值相加, 导致计数错误.</del> 已经支持添加 <code>[resume]</code>, <code>[start=]</code> 等可选参数</p></li>
<li><p><del>不支持嵌套</del>, 已使用 <code>\enit@depth</code> 命令来判断层级, 嵌套的问题被解决.</p></li>
</ol>
<h2 id="后续">后续</h2>
<p>我在 <code>tex.se</code> 上还看到到了一种实现方式, 直接使用 <code>\label</code> 来进行辅助文件的输出, 这里是回答的<a target="_blank" rel="noopener" href="https://tex.stackexchange.com/a/74617/180617">地址</a>, 这个方法局限性较大, 不采用</p>
<p>这个需求应该是结束了, 欢迎测试和评论 :&gt;</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div></div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/img/avatar/wechatpay.png" alt="Zhonghao Sun 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/img/avatar/alipay.jpg" alt="Zhonghao Sun 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Zhonghao Sun
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://syvshc.gitee.io/2021-11-10-compute-total-items/" title="统计一节中的列表项数量">https://syvshc.gitee.io/2021-11-10-compute-total-items/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/LaTeX/" rel="tag"># LaTeX</a>
              <a href="/tags/%E7%A4%BA%E4%BE%8B/" rel="tag"># 示例</a>
              <a href="/tags/%E5%88%97%E8%A1%A8%E7%9B%B8%E5%85%B3/" rel="tag"># 列表相关</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021-10-02-extcite/" rel="prev" title="一种特殊的参考文献引用样式的设置">
                  <i class="fa fa-chevron-left"></i> 一种特殊的参考文献引用样式的设置
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021-11-18-backup-blogs/" rel="next" title="备份博客内容">
                  备份博客内容 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments gitalk-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Syvshc</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">68k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">1:02</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"ams","js":{"url":"https://cdn.jsdelivr.net/npm/mathjax@3.2.0/es5/tex-mml-chtml.js","integrity":"sha256-r+3itOMtGGjap0x+10hu6jW/gZCzxHsoKrOd7gyRSGY="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1.7.2/dist/gitalk.css" integrity="sha256-AJnUHL7dBv6PGaeyPQJcgQPDjt/Hn/PvYZde1iqfp8U=" crossorigin="anonymous">

<script class="next-config" data-name="gitalk" type="application/json">{"enable":true,"github_id":"syvshc","repo":"syvshc.github.io","client_id":"da0debc4d456cb5f3371","client_secret":"14ec65d19a969abe01994143eff233a4180484aa","admin_user":"syvshc","distraction_free_mode":true,"proxy":"https://cors-anywhere.azm.workers.dev/https://github.com/login/oauth/access_token","language":"zh-CN","js":{"url":"https://cdn.jsdelivr.net/npm/gitalk@1.7.2/dist/gitalk.min.js","integrity":"sha256-Pmj85ojLaPOWwRtlMJwmezB/Qg8BzvJp5eTzvXaYAfA="},"path_md5":"a10958b388027cbc4160042c4c452f44"}</script>
<script src="/js/third-party/comments/gitalk.js"></script>

</body>
</html>
